package com.calendar.master.learncompose

import android.os.Bundle
import android.util.Log
import android.util.Size
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.BlurredEdgeTreatment
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.*
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.*
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.calendar.master.learncompose.ui.theme.LearnComposeTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            LearnComposeTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    addGradientFrame()
                }
            }
        }
    }
}

@Composable
fun NoLeadingZeroes() {
    var input by rememberSaveable { mutableStateOf("") }
    val textStyle = TextStyle()
    TextField(
        value = input,
        onValueChange = { newText ->
            Log.e("steve","输入：$newText")
            input = newText.trimStart { it == '0' }
        }, placeholder = {
            Text(text = "请输入账号")
        }
    )
}

@Composable
fun addFrame() {
    Column {
        val borderWidth = 4.dp
        Image(
            painter = painterResource(id = R.mipmap.chahua),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(150.dp)
                .border(
                    BorderStroke(borderWidth, Color.Red),
                    CircleShape
                )
                .padding(borderWidth)
                .clip(CircleShape)
        )
    }
}

@Composable
fun addGradientFrame() {
    val rainbowColorsBrush = remember {
        Brush.sweepGradient(
            listOf(
                Color(0xFF9575CD),
                Color(0xFFBA68C8),
                Color(0xFFE57373),
                Color(0xFFFFB74D),
                Color(0xFFFFF176),
                Color(0xFFAED581),
                Color(0xFF4DD0E1),
                Color(0xFF9575CD)
            )
        )
    }
    val borderWidth = 4.dp
    Column(verticalArrangement = Arrangement.Center) {
        Image(
            painter = painterResource(id = R.mipmap.chahua),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(150.dp)
                .border(
                    BorderStroke(borderWidth, rainbowColorsBrush),
                    CircleShape
                )
                .padding(borderWidth)
                .clip(CircleShape)
        )
        Image(
            painter = painterResource(id = R.mipmap.chahua),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier.aspectRatio(16f/9f).padding(bottom = 10f.dp)
        )
        Image(
            painter = painterResource(id = R.mipmap.chahua),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(150.dp)
                .blur(
                    radiusX = 10.dp,
                    radiusY = 10.dp,
                    edgeTreatment = BlurredEdgeTreatment(RoundedCornerShape(8.dp))
                )
        )
    }
}

@Composable
fun customShape() {
    class SquashedOval : Shape {
        override fun createOutline(
            size: androidx.compose.ui.geometry.Size,
            layoutDirection: LayoutDirection,
            density: Density
        ): Outline {
            val path = Path().apply {
                // We create an Oval that starts at ¼ of the width, and ends at ¾ of the width of the container.
                addOval(
                    Rect(
                        left = size.width / 4f,
                        top = 0f,
                        right = size.width * 3 / 4f,
                        bottom = size.height
                    )
                )
            }
            return Outline.Generic(path = path)
        }
    }

    Column() {
        Image(
            painter = painterResource(id = R.mipmap.chahua),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(200f.dp)
                .clip(SquashedOval())
        )
    }
}

@Composable
fun customImage() {
    val imageUrl = "https://img1.baidu.com/it/u=1310564963,1641173348&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800"
    val imageModifier = Modifier
        .size(150.dp)
        .border(BorderStroke(1.dp, Color.Black))
        .background(Color.Yellow)
        .clip(RoundedCornerShape(16f.dp))
    Column(verticalArrangement = Arrangement.Top) {
        AsyncImage(model = imageUrl, contentDescription = null,
            modifier = imageModifier,
            contentScale = ContentScale.Crop)
    }
}

@Composable
fun rowAndColumn() {
    val imageUrl = "https://img1.baidu.com/it/u=1310564963,1641173348&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800"
    val imageModifier = Modifier
        .size(150.dp)
        .border(BorderStroke(1.dp, Color.Black))
        .background(Color.Yellow)
        .clip(CircleShape)
    Box(modifier = Modifier.fillMaxWidth()) {
        Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
            Image(painter = painterResource(id = R.mipmap.chahua),
                contentDescription = "头像",
                modifier = Modifier
                    .size(100f.dp)
                    .clip(CircleShape))
            Column(verticalArrangement = Arrangement.Top) {
                Text(text = "昵称")
                Text(text = "ID：1234567890123")
                AsyncImage(model = imageUrl, contentDescription = null,
                    modifier = imageModifier,
                contentScale = ContentScale.Crop)
            }
        }
    }
}

@Composable
fun PasswordTextField() {
    var password by rememberSaveable { mutableStateOf("") }
    Box(modifier = Modifier
        .fillMaxWidth()
        .height(50f.dp), Alignment.Center) {
        TextField(
            value = password,
            onValueChange = { password = it },
            label = { Text("Enter password") },
            visualTransformation = PasswordVisualTransformation(),
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
            modifier = Modifier.fillMaxWidth()
        )
    }
}

@Composable
fun input() {
    Column() {
        var text by remember { mutableStateOf("Hello") }
        TextField(value = text, onValueChange = {
            Log.d("steve","input ： $it")
            text = it
        }, maxLines = 3, textStyle = TextStyle(color = Color.Red, fontWeight = FontWeight.Bold), modifier = Modifier.padding(20.dp))

        var outText by remember { mutableStateOf("out:") }
        OutlinedTextField(value = outText, label = { Text(text = "Label")},onValueChange = {
            outText = it
        })
    }
}

@Composable
fun LongText() {
    Text("hello ".repeat(50), maxLines = 2, style = TextStyle(color = Color.Yellow))
}

@OptIn(ExperimentalTextApi::class)
@Composable
fun OverflowedText() {
    Text("Hello Compose".repeat(50), maxLines = 2,
        overflow = TextOverflow.Ellipsis, modifier = Modifier.width(200.dp), style = TextStyle()
    )
}

@Composable
fun SimpleClickableText() {
    ClickableText(
        text = AnnotatedString("Click Me"),
        onClick = { offset ->
            Log.d("ClickableText", "$offset -th character is clicked.")
        }
    )
}

@Composable
fun AnnotatedClickableText() {
    val annotatedText = buildAnnotatedString {
        append("Click ")

        // We attach this *URL* annotation to the following content
        // until `pop()` is called
        pushStringAnnotation(tag = "URL",
            annotation = "https://www.baidu.com")
        withStyle(style = SpanStyle(color = Color.Blue,
            fontWeight = FontWeight.Bold)) {
            append("here")
        }

        pop()
    }

    ClickableText(
        text = annotatedText,
        onClick = { offset ->
            // We check if there is an *URL* annotation attached to the text
            // at the clicked position
            annotatedText.getStringAnnotations(tag = "URL", start = offset,
                end = offset)
                .firstOrNull()?.let { annotation ->
                    // If yes, we log its value
                    Log.e("Clicked URL", annotation.item)
                }
        }
    )
}

@Composable
fun TextShadow() {
    val offset = Offset(5.0f, 10.0f)
    Text(
        text = "Hello world!",
        style = TextStyle(
            fontSize = 24.sp,
            shadow = Shadow(
                color = Color.Blue,
                offset = offset,
                blurRadius = 3f
            )
        )
    )
}

@Composable
fun DifferentFonts() {
    Column {
        Text("Hello World", fontFamily = FontFamily.Serif)
        Text("Hello World", fontFamily = FontFamily.SansSerif)
    }
}

@Composable
fun Greeting(name: String) {
    Column( verticalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth()) {
        val textStyle = TextStyle(
            fontSize = 10.sp,
            textAlign = TextAlign.Start,
            fontStyle = FontStyle.Italic,
            fontWeight = FontWeight.Bold
        )
        // 将字符串颜色值转换为Color对象
        val colorString = "#FF0000" // 红色
        val colorRed = Color(android.graphics.Color.parseColor(colorString))
        Text(text = "Hello Compose", color = colorRed, style = textStyle,modifier = Modifier
            .padding(start = 10.dp, end = 10.dp))
        Text(text = "Hello $name!1", color = Color.Blue, style = textStyle, modifier = Modifier
            .padding(start = 10.dp, end = 10.dp)
            .fillMaxWidth()
            .clickable {

            })
        Text(text = buildAnnotatedString {
            withStyle(style = SpanStyle(color = Color.DarkGray, fontSize = 20.sp, background = Color.Gray)) {
                append("这是")
            }
            withStyle(style = SpanStyle(color = Color.Green, fontSize = 15.sp)) {
                append("绿色")
            }
            withStyle(style = ParagraphStyle()) {
                withStyle(style = SpanStyle(color = Color.Red, fontSize = 18.sp)) {
                    append("重要的话另起一行")
                }
            }
        }, color = colorRed)
//        Text(text = stringResource(R.string.hello))
    }
}

@Composable
fun boxView() {
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Text(text = "BoxView")
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreviewTest() {
    LearnComposeTheme {
        Column {
            rowAndColumn()
        }
    }
}